package com.jiangyifen.robot.bgthread;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jiangyifen.robot.core.Robot;
import com.jiangyifen.robot.service.RobotService;

@Component
public class RobotRegisterThread extends Thread{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RobotService robotService;
	
	public RobotRegisterThread(){
		this.setName(this.getClass().getSimpleName());
		this.setDaemon(true);
		this.start();
		logger.info(this.getClass().getSimpleName() + " started !");
	}

	@Override
	public void run(){
		try{
			while(true){
				sleep(300000);
				Map<Long, Robot> allRobots = robotService.getAllRobots();
				for(Robot r: allRobots.values()){
					r.register();
				}
			}
		}catch(Exception e){
			logger.error(e.getMessage(),e);
		}
	}
	
}
